<%#
 Copyright 2013-2020 the original author or authors from the JHipster project.

 This file is part of the JHipster project, see https://www.jhipster.tech/
 for more information.

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-%>
package <%= packageName %>.web.rest;

<%_ if (databaseType === 'cassandra') { _%>
import <%= packageName %>.AbstractCassandraTest;
<%_ } _%>
<%_ if (databaseType === 'neo4j') { _%>
import <%= packageName %>.AbstractNeo4jIT;
<%_ } _%>
<%_ if (cacheProvider === 'redis') { _%>
import <%= packageName %>.RedisTestContainerExtension;
<%_ } _%>
<%_ if (reactiveSqlTestContainers) { _%>
import <%= packageName %>.ReactiveSqlTestContainerExtension;
<%_ } _%>
import <%= packageName %>.<%= mainClass %>;
<%_ if (databaseType === 'sql' && reactive) { _%>
import <%= packageName %>.config.Constants;
<%_ } _%>
<%_ if (authenticationType === 'oauth2') { _%>
import <%= packageName %>.config.TestSecurityConfiguration;
<%_ } _%>
<%_ if (databaseType !== 'cassandra' && databaseType !== 'couchbase') { _%>
import <%= packageName %>.domain.Authority;
<%_ } _%>
import <%= packageName %>.domain.<%= asEntity('User') %>;
import <%= packageName %>.repository.UserRepository;
<%_ if (searchEngine === 'elasticsearch') { _%>
import <%= packageName %>.repository.search.UserSearchRepository;
<%_ } _%>
import <%= packageName %>.security.AuthoritiesConstants;
import <%= packageName %>.service.dto.<%= asDto('User') %>;
<%_ if (databaseType === 'sql' && reactive) { _%>
import <%= packageName %>.service.EntityManager;
<%_ } _%>
import <%= packageName %>.service.mapper.UserMapper;
<%_ if (authenticationType !== 'oauth2') { _%>
import <%= packageName %>.web.rest.vm.ManagedUserVM;
<%_ } _%>
import org.apache.commons.lang3.RandomStringUtils;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
<%_ if (databaseType === 'neo4j' || cacheProvider === 'redis' || reactiveSqlTestContainers) { _%>
import org.junit.jupiter.api.extension.ExtendWith;
<%_ } _%>
import org.springframework.beans.factory.annotation.Autowired;
<%_ if (!reactive) { _%>
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
<%_ } else { _%>
import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient;
<%_ } _%>
import org.springframework.boot.test.context.SpringBootTest;
<%_ if (cacheManagerIsAvailable === true) { _%>
import org.springframework.cache.CacheManager;
<%_ } _%>
<%_ if (cacheProvider === 'memcached' ) { _%>
import org.springframework.cache.support.NoOpCacheManager;
<%_ } _%>
import org.springframework.http.MediaType;
<%_ if (databaseType === 'couchbase') { _%>
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.test.context.TestSecurityContextHolder;
<%_ } _%>
import org.springframework.security.test.context.support.WithMockUser;
<%_ if (reactive) { _%>
import org.springframework.test.web.reactive.server.WebTestClient;
<%_ } _%>
<%_ if (!reactive) { _%>
import org.springframework.test.web.servlet.MockMvc;
<%_ } _%>
<%_ if (databaseType === 'sql' && !reactive) { _%>
import org.springframework.transaction.annotation.Transactional;
<%_ } _%>
<%_ if (reactive && searchEngine === 'elasticsearch') { _%>
import reactor.core.publisher.Mono;
<%_ } _%>

<%_ if (databaseType === 'sql' && !reactive) { _%>
import javax.persistence.EntityManager;
<%_ } _%>
<%_ if (databaseType !== 'cassandra') { _%>
import java.time.Instant;
<%_ } _%>
import java.util.*;
import java.util.function.Consumer;
<%_ if (databaseType === 'cassandra' || databaseType === 'couchbase') { _%>
import java.util.stream.Collectors;
import java.util.stream.Stream;
<%_ } _%>
<%_ if (databaseType === 'cassandra') { _%>
import java.util.UUID;
<%_ } _%>

import static org.assertj.core.api.Assertions.assertThat;
<%_ if (reactive && searchEngine === 'elasticsearch') { _%>
import static org.mockito.Mockito.*;
<%_ } _%>
<%_ if (!reactive) { _%>
    <%_ if (databaseType === 'sql' || databaseType === 'mongodb' || databaseType === 'neo4j' || databaseType === 'couchbase') { _%>
import static org.hamcrest.Matchers.hasItems;
    <%_ } _%>
import static org.hamcrest.Matchers.hasItem;
    <%_ if (testsNeedCsrf) { _%>
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
    <%_ } _%>
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
<%_ } else if (testsNeedCsrf) { _%>
import static org.springframework.security.test.web.reactive.server.SecurityMockServerConfigurers.csrf;
<%_ } _%>

/**
 * Integration tests for the {@link UserResource} REST controller.
 */
<%_ if (!reactive) { _%>
@AutoConfigureMockMvc
<%_ } else { _%>
@AutoConfigureWebTestClient
<%_ } _%>
@WithMockUser(authorities = AuthoritiesConstants.ADMIN)
<%_ if (authenticationType === 'oauth2') { _%>
@SpringBootTest(classes = {<%= mainClass %>.class, TestSecurityConfiguration.class})
<%_ } else { _%>
@SpringBootTest(classes = <%= mainClass %>.class)
<%_ } _%>
<%_ if (cacheProvider === 'redis') { _%>
@ExtendWith(RedisTestContainerExtension.class)
<%_ } _%>
<%_ if (databaseType === 'neo4j') { _%>
@ExtendWith(AbstractNeo4jIT.class)
<%_ } _%>
<%_ if (reactiveSqlTestContainers) { _%>
@ExtendWith(ReactiveSqlTestContainerExtension.class)
<%_ } _%>
class UserResourceIT <% if (databaseType === 'cassandra') { %>extends AbstractCassandraTest <% } %>{

    private static final String DEFAULT_LOGIN = "johndoe";
    <%_ if (authenticationType !== 'oauth2') { _%>
    private static final String UPDATED_LOGIN = "jhipster";
    <%_ } _%>

    <%_ if (databaseType === 'sql' && authenticationType !== 'oauth2') { _%>
    private static final Long DEFAULT_ID = 1L;
    <%_ } else if (databaseType === 'couchbase') { _%>
    private static final String DEFAULT_ID = <%= asEntity('User') %>.PREFIX + DEFAULT_LOGIN;
    <%_ } else { _%>
    private static final String DEFAULT_ID = "id1";
    <%_ } _%>
    <%_ if (authenticationType !== 'oauth2') { _%>

    private static final String DEFAULT_PASSWORD = "passjohndoe";
    private static final String UPDATED_PASSWORD = "passjhipster";
    <%_ } _%>

    private static final String DEFAULT_EMAIL = "johndoe@localhost";
    <%_ if (authenticationType !== 'oauth2') { _%>
    private static final String UPDATED_EMAIL = "jhipster@localhost";
    <%_ } _%>

    private static final String DEFAULT_FIRSTNAME = "john";
    <%_ if (authenticationType !== 'oauth2') { _%>
    private static final String UPDATED_FIRSTNAME = "jhipsterFirstName";
    <%_ } _%>

    private static final String DEFAULT_LASTNAME = "doe";
    <%_ if (authenticationType !== 'oauth2') { _%>
    private static final String UPDATED_LASTNAME = "jhipsterLastName";
    <%_ } _%>

    <%_ if (databaseType !== 'cassandra') { _%>
    private static final String DEFAULT_IMAGEURL = "http://placehold.it/50x50";
    <%_ if (authenticationType !== 'oauth2') { _%>
    private static final String UPDATED_IMAGEURL = "http://placehold.it/40x40";
    <%_ } _%>

    <%_ } _%>
    private static final String DEFAULT_LANGKEY = "en";
    <%_ if (authenticationType !== 'oauth2') { _%>
    private static final String UPDATED_LANGKEY = "fr";
    <%_ } _%>

    @Autowired
    private UserRepository userRepository;
    <%_ if (searchEngine === 'elasticsearch') { _%>

    /**
     * This repository is mocked in the <%= packageName %>.repository.search test package.
     *
     * @see <%= packageName %>.repository.search.UserSearchRepositoryMockConfiguration
     */
    @Autowired
    private UserSearchRepository mockUserSearchRepository;
    <%_ } _%>

    @Autowired
    private UserMapper userMapper;
    <%_ if (databaseType === 'sql') { _%>

    @Autowired
    private EntityManager em;
    <%_ } _%>
    <%_ if (cacheManagerIsAvailable === true) { _%>

    @Autowired
    private CacheManager cacheManager;
    <%_ } _%>

    @Autowired
    <%_ if (reactive) { _%>
    private WebTestClient webTestClient;
    <%_ } else { _%>
    private MockMvc restUserMockMvc;
    <%_ } _%>

    private <%= asEntity('User') %> user;

    <%_ if (cacheManagerIsAvailable === true) { _%>
    @BeforeEach
    public void setup() {
        cacheManager.getCache(UserRepository.USERS_BY_LOGIN_CACHE).clear();
        cacheManager.getCache(UserRepository.USERS_BY_EMAIL_CACHE).clear();
    }

    <%_ } _%>
    <%_ if (reactive && testsNeedCsrf) { _%>
    @BeforeEach
    public void setupCsrf() {
        webTestClient = webTestClient.mutateWith(csrf());
    }

    <%_ } _%>
    /**
     * Create a User.
     *
     * This is a static method, as tests for other entities might also need it,
     * if they test an entity which has a required relationship to the User entity.
     */
    public static <%= asEntity('User') %> createEntity(<% if (databaseType === 'sql') { %>EntityManager em<% } %>) {
        <%= asEntity('User') %> user = new <%= asEntity('User') %>();
        <%_ if (databaseType === 'cassandra' || (authenticationType === 'oauth2' && databaseType !== 'couchbase')) { _%>
        user.setId(UUID.randomUUID().toString());
        <%_ } _%>
        user.setLogin(DEFAULT_LOGIN<% if (databaseType === 'sql') { %> + RandomStringUtils.randomAlphabetic(5)<% } %>);
        <%_ if (authenticationType !== 'oauth2') { _%>
        user.setPassword(RandomStringUtils.random(60));
        <%_ } _%>
        user.setActivated(true);
        user.setEmail(<% if (databaseType === 'sql') { %>RandomStringUtils.randomAlphabetic(5) + <% } %>DEFAULT_EMAIL);
        user.setFirstName(DEFAULT_FIRSTNAME);
        user.setLastName(DEFAULT_LASTNAME);
        <%_ if (databaseType !== 'cassandra') { _%>
        user.setImageUrl(DEFAULT_IMAGEURL);
        <%_ } _%>
        user.setLangKey(DEFAULT_LANGKEY);
        <%_ if (databaseType === 'sql' && reactive) { _%>
        user.setCreatedBy(Constants.SYSTEM_ACCOUNT);
        <%_ } _%>
        return user;
    }
    <%_ if (databaseType === 'sql' && reactive) { _%>

    /**
     * Delete all the users from the database.
     */
    public static void deleteEntities(EntityManager em) {
        try {
            em.deleteAll("<%= jhiTablePrefix %>_user_authority").block();
            em.deleteAll(User.class).block();
        } catch (Exception e) {
            // It can fail, if other entities are still referring this - it will be removed later.
        }
    }
    <%_ } _%>

    @BeforeEach
    public void initTest() {
        <%_ if (databaseType === 'sql' && reactive) { _%>
        userRepository.deleteAllUserAuthorities().block();
        <%_ } _%>
        <%_ if (databaseType !== 'sql' || reactive) { _%>
        userRepository.deleteAll()<% if (reactive) { %>.block()<% } %>;
        <%_ } _%>
        user = createEntity(<% if (databaseType === 'sql' ) { %>em<% } _%>);
        <%_ if (databaseType === 'sql') { _%>
        user.setLogin(DEFAULT_LOGIN);
        user.setEmail(DEFAULT_EMAIL);
        <%_ } _%>
    }
<%_ if (authenticationType !== 'oauth2') { _%>

    @Test
    <%_ if (databaseType === 'sql' && !reactive) { _%>
    @Transactional
    <%_ } _%>
    void createUser() throws Exception {
        int databaseSizeBeforeCreate = userRepository.findAll()<% if (reactive) { %>
            .collectList().block()<% } %>.size();
        <%_ if (searchEngine === 'elasticsearch' && reactive) { _%>
        // Configure the mock search repository
        when(mockUserSearchRepository.save(any()))
            .thenAnswer(invocation -> Mono.just(invocation.getArgument(0)));
        <%_ } _%>

        // Create the User
        ManagedUserVM managedUserVM = new ManagedUserVM();
        managedUserVM.setLogin(DEFAULT_LOGIN);
        managedUserVM.setPassword(DEFAULT_PASSWORD);
        managedUserVM.setFirstName(DEFAULT_FIRSTNAME);
        managedUserVM.setLastName(DEFAULT_LASTNAME);
        managedUserVM.setEmail(DEFAULT_EMAIL);
        managedUserVM.setActivated(true);
        <%_ if (databaseType !== 'cassandra') { _%>
        managedUserVM.setImageUrl(DEFAULT_IMAGEURL);
        <%_ } _%>
        managedUserVM.setLangKey(DEFAULT_LANGKEY);
        managedUserVM.setAuthorities(Collections.singleton(AuthoritiesConstants.USER));

        <%_ if (!reactive) { _%>
        restUserMockMvc.perform(post("/api/users")
            .contentType(MediaType.APPLICATION_JSON)
            .content(TestUtil.convertObjectToJsonBytes(managedUserVM))<% if (testsNeedCsrf) { %>
            .with(csrf())<% } %>)
            .andExpect(status().isCreated());
        <%_ } else { _%>
        webTestClient.post().uri("/api/users")
            .contentType(MediaType.APPLICATION_JSON)
            .bodyValue(TestUtil.convertObjectToJsonBytes(managedUserVM))
            .exchange()
            .expectStatus().isCreated();
        <%_ } _%>

        // Validate the User in the database
        assertPersistedUsers(users -> {
            assertThat(users).hasSize(databaseSizeBeforeCreate + 1);
            <%= asEntity('User') %> testUser = users.get(users.size() - 1);
            assertThat(testUser.getLogin()).isEqualTo(DEFAULT_LOGIN);
            assertThat(testUser.getFirstName()).isEqualTo(DEFAULT_FIRSTNAME);
            assertThat(testUser.getLastName()).isEqualTo(DEFAULT_LASTNAME);
            assertThat(testUser.getEmail()).isEqualTo(DEFAULT_EMAIL);
            <%_ if (databaseType !== 'cassandra') { _%>
            assertThat(testUser.getImageUrl()).isEqualTo(DEFAULT_IMAGEURL);
            <%_ } _%>
            assertThat(testUser.getLangKey()).isEqualTo(DEFAULT_LANGKEY);
        });
    }

    @Test
    <%_ if (databaseType === 'sql' && !reactive) { _%>
    @Transactional
    <%_ } _%>
    void createUserWithExistingId() throws Exception {
        int databaseSizeBeforeCreate = userRepository.findAll()<% if (reactive) { %>
            .collectList().block()<% } %>.size();

        ManagedUserVM managedUserVM = new ManagedUserVM();
        <%_ if (databaseType === 'cassandra') { _%>
        managedUserVM.setId(UUID.randomUUID().toString());
        <%_ } else if (databaseType === 'mongodb' || databaseType === 'neo4j' || databaseType === 'couchbase') { _%>
        managedUserVM.setId("1L");
        <%_ } else { _%>
        managedUserVM.setId(1L);
        <%_ } _%>
        managedUserVM.setLogin(DEFAULT_LOGIN);
        managedUserVM.setPassword(DEFAULT_PASSWORD);
        managedUserVM.setFirstName(DEFAULT_FIRSTNAME);
        managedUserVM.setLastName(DEFAULT_LASTNAME);
        managedUserVM.setEmail(DEFAULT_EMAIL);
        managedUserVM.setActivated(true);
        <%_ if (databaseType !== 'cassandra') { _%>
        managedUserVM.setImageUrl(DEFAULT_IMAGEURL);
        <%_ } _%>
        managedUserVM.setLangKey(DEFAULT_LANGKEY);
        managedUserVM.setAuthorities(Collections.singleton(AuthoritiesConstants.USER));

        // An entity with an existing ID cannot be created, so this API call must fail
        <%_ if (!reactive) { _%>
        restUserMockMvc.perform(post("/api/users")
            .contentType(MediaType.APPLICATION_JSON)
            .content(TestUtil.convertObjectToJsonBytes(managedUserVM))<% if (testsNeedCsrf) { %>
            .with(csrf())<% } %>)
            .andExpect(status().isBadRequest());
        <%_ } else { _%>
        webTestClient.post().uri("/api/users")
            .contentType(MediaType.APPLICATION_JSON)
            .bodyValue(TestUtil.convertObjectToJsonBytes(managedUserVM))
            .exchange()
            .expectStatus().isBadRequest();
        <%_ } _%>

        // Validate the User in the database
        assertPersistedUsers(users -> assertThat(users).hasSize(databaseSizeBeforeCreate));
    }

    @Test
    <%_ if (databaseType === 'sql' && !reactive) { _%>
    @Transactional
    <%_ } _%>
    void createUserWithExistingLogin() throws Exception {
        // Initialize the database
        userRepository.save<% if (databaseType === 'sql' && !reactive) { %>AndFlush<% } %>(user)<% if (reactive) { %>.block()<% } %>;
        <%_ if (searchEngine === 'elasticsearch') { _%>
        mockUserSearchRepository.save(user);
        <%_ } _%>
        int databaseSizeBeforeCreate = userRepository.findAll()<% if (reactive) { %>
            .collectList().block()<% } %>.size();

        ManagedUserVM managedUserVM = new ManagedUserVM();
        managedUserVM.setLogin(DEFAULT_LOGIN);// this login should already be used
        managedUserVM.setPassword(DEFAULT_PASSWORD);
        managedUserVM.setFirstName(DEFAULT_FIRSTNAME);
        managedUserVM.setLastName(DEFAULT_LASTNAME);
        managedUserVM.setEmail("anothermail@localhost");
        managedUserVM.setActivated(true);
        <%_ if (databaseType !== 'cassandra') { _%>
        managedUserVM.setImageUrl(DEFAULT_IMAGEURL);
        <%_ } _%>
        managedUserVM.setLangKey(DEFAULT_LANGKEY);
        managedUserVM.setAuthorities(Collections.singleton(AuthoritiesConstants.USER));

        // Create the User
        <%_ if (!reactive) { _%>
        restUserMockMvc.perform(post("/api/users")
            .contentType(MediaType.APPLICATION_JSON)
            .content(TestUtil.convertObjectToJsonBytes(managedUserVM))<% if (testsNeedCsrf) { %>
            .with(csrf())<% } %>)
            .andExpect(status().isBadRequest());
        <%_ } else { _%>
        webTestClient.post().uri("/api/users")
            .contentType(MediaType.APPLICATION_JSON)
            .bodyValue(TestUtil.convertObjectToJsonBytes(managedUserVM))
            .exchange()
            .expectStatus().isBadRequest();
        <%_ } _%>

        // Validate the User in the database
        assertPersistedUsers(users -> assertThat(users).hasSize(databaseSizeBeforeCreate));
    }

    @Test
    <%_ if (databaseType === 'sql' && !reactive) { _%>
    @Transactional
    <%_ } _%>
    void createUserWithExistingEmail() throws Exception {
        // Initialize the database
        userRepository.save<% if (databaseType === 'sql' && !reactive) { %>AndFlush<% } %>(user)<% if (reactive) { %>.block()<% } %>;
        <%_ if (searchEngine === 'elasticsearch') { _%>
        mockUserSearchRepository.save(user);
        <%_ } _%>
        int databaseSizeBeforeCreate = userRepository.findAll()<% if (reactive) { %>
            .collectList().block()<% } %>.size();

        ManagedUserVM managedUserVM = new ManagedUserVM();
        managedUserVM.setLogin("anotherlogin");
        managedUserVM.setPassword(DEFAULT_PASSWORD);
        managedUserVM.setFirstName(DEFAULT_FIRSTNAME);
        managedUserVM.setLastName(DEFAULT_LASTNAME);
        managedUserVM.setEmail(DEFAULT_EMAIL);// this email should already be used
        managedUserVM.setActivated(true);
        <%_ if (databaseType !== 'cassandra') { _%>
        managedUserVM.setImageUrl(DEFAULT_IMAGEURL);
        <%_ } _%>
        managedUserVM.setLangKey(DEFAULT_LANGKEY);
        managedUserVM.setAuthorities(Collections.singleton(AuthoritiesConstants.USER));

        // Create the User
        <%_ if (!reactive) { _%>
        restUserMockMvc.perform(post("/api/users")
            .contentType(MediaType.APPLICATION_JSON)
            .content(TestUtil.convertObjectToJsonBytes(managedUserVM))<% if (testsNeedCsrf) { %>
            .with(csrf())<% } %>)
            .andExpect(status().isBadRequest());
        <%_ } else { _%>
        webTestClient.post().uri("/api/users")
            .contentType(MediaType.APPLICATION_JSON)
            .bodyValue(TestUtil.convertObjectToJsonBytes(managedUserVM))
            .exchange()
            .expectStatus().isBadRequest();
        <%_ } _%>

        // Validate the User in the database
        assertPersistedUsers(users -> assertThat(users).hasSize(databaseSizeBeforeCreate));
    }
<%_ } _%>

    @Test
    <%_ if (databaseType === 'sql' && !reactive) { _%>
    @Transactional
    <%_ } _%>
    void getAllUsers()<% if (!reactive) { %> throws Exception<% } %> {
        // Initialize the database
        userRepository.<% if (databaseType === 'sql' && reactive && authenticationType === 'oauth2') { %>create<% } else { %>save<% } %><% if (databaseType === 'sql' && !reactive) { %>AndFlush<% } %>(user)<% if (reactive) { %>.block()<% } %>;

        // Get all the users
        <%_ if (!reactive) { _%>
        restUserMockMvc.perform(get("/api/users<% if (databaseType === 'sql') { %>?sort=id,desc<% } %>")
            .accept(MediaType.APPLICATION_JSON))
            .andExpect(status().isOk())
            .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
            .andExpect(jsonPath("$.[*].login").value(hasItem(DEFAULT_LOGIN)))
            .andExpect(jsonPath("$.[*].firstName").value(hasItem(DEFAULT_FIRSTNAME)))
            .andExpect(jsonPath("$.[*].lastName").value(hasItem(DEFAULT_LASTNAME)))
            .andExpect(jsonPath("$.[*].email").value(hasItem(DEFAULT_EMAIL)))
            <%_ if (databaseType !== 'cassandra') { _%>
            .andExpect(jsonPath("$.[*].imageUrl").value(hasItem(DEFAULT_IMAGEURL)))
            <%_ } _%>
            .andExpect(jsonPath("$.[*].langKey").value(hasItem(DEFAULT_LANGKEY)));
        <%_ } else { _%>
        <%= asDto('User') %> foundUser = webTestClient.get().uri("/api/users?sort=id,DESC")
            .accept(MediaType.APPLICATION_JSON)
            .exchange()
            .expectStatus().isOk()
            .expectHeader().contentType(MediaType.APPLICATION_JSON)
            .returnResult(<%= asDto('User') %>.class).getResponseBody().blockFirst();

        assertThat(foundUser.getLogin()).isEqualTo(DEFAULT_LOGIN);
        assertThat(foundUser.getFirstName()).isEqualTo(DEFAULT_FIRSTNAME);
        assertThat(foundUser.getLastName()).isEqualTo(DEFAULT_LASTNAME);
        assertThat(foundUser.getEmail()).isEqualTo(DEFAULT_EMAIL);
        <%_ if (databaseType !== 'cassandra') { _%>
        assertThat(foundUser.getImageUrl()).isEqualTo(DEFAULT_IMAGEURL);
        <%_ } _%>
        assertThat(foundUser.getLangKey()).isEqualTo(DEFAULT_LANGKEY);
        <%_ } _%>
    }

    <%_ if (databaseType === 'sql' && authenticationType !== 'oauth2') { _%>
    @Test
    <%_ if (!reactive) { _%>
    @Transactional
    <%_ } _%>
    void getAllUsersSortedByParameters() throws Exception {
        // Initialize the database
        userRepository.<% if (reactive) { %>save<% } else { %>saveAndFlush<% } %>(user)<% if (reactive) { %>.block()<% } %>;

        <%_ if (!reactive) { _%>
            restUserMockMvc.perform(get("/api/users?sort=resetKey,desc").accept(MediaType.APPLICATION_JSON)).andExpect(status().isBadRequest());
            restUserMockMvc.perform(get("/api/users?sort=password,desc").accept(MediaType.APPLICATION_JSON)).andExpect(status().isBadRequest());
            restUserMockMvc.perform(get("/api/users?sort=resetKey,id,desc").accept(MediaType.APPLICATION_JSON)).andExpect(status().isBadRequest());
            restUserMockMvc.perform(get("/api/users?sort=id,desc").accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk());
        <%_ } else { _%>
            webTestClient.get().uri("/api/users?sort=resetKey,DESC").accept(MediaType.APPLICATION_JSON).exchange().expectStatus().isBadRequest();
            webTestClient.get().uri("/api/users?sort=password,DESC").accept(MediaType.APPLICATION_JSON).exchange().expectStatus().isBadRequest();
            webTestClient.get().uri("/api/users?sort=resetKey,id,DESC").accept(MediaType.APPLICATION_JSON).exchange().expectStatus().isBadRequest();
            webTestClient.get().uri("/api/users?sort=id,DESC").accept(MediaType.APPLICATION_JSON).exchange().expectStatus().isOk();
        <%_ } _%>
    }
    <%_ } _%>

    @Test
    <%_ if (databaseType === 'sql' && !reactive) { _%>
    @Transactional
    <%_ } _%>
    void getUser()<% if (!reactive) { %> throws Exception<% } %> {
        // Initialize the database
        userRepository.<% if (databaseType === 'sql' && reactive && authenticationType === 'oauth2') { %>create<% } else { %>save<% } %><% if (databaseType === 'sql' && !reactive) { %>AndFlush<% } %>(user)<% if (reactive) { %>.block()<% } %>;
        <%_ if (searchEngine === 'elasticsearch') { _%>
        mockUserSearchRepository.save(user);
        <%_ } _%>
        <%_ if (cacheManagerIsAvailable === true) { _%>

        assertThat(cacheManager.getCache(UserRepository.USERS_BY_LOGIN_CACHE).get(user.getLogin())).isNull();
        <%_ } _%>

        // Get the user
        <%_ if (!reactive) { _%>
        restUserMockMvc.perform(get("/api/users/{login}", user.getLogin()))
            .andExpect(status().isOk())
            .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
            .andExpect(jsonPath("$.login").value(user.getLogin()))
            .andExpect(jsonPath("$.firstName").value(DEFAULT_FIRSTNAME))
            .andExpect(jsonPath("$.lastName").value(DEFAULT_LASTNAME))
            .andExpect(jsonPath("$.email").value(DEFAULT_EMAIL))
            <%_ if (databaseType !== 'cassandra') { _%>
            .andExpect(jsonPath("$.imageUrl").value(DEFAULT_IMAGEURL))
            <%_ } _%>
            .andExpect(jsonPath("$.langKey").value(DEFAULT_LANGKEY));
        <%_ } else { _%>
        webTestClient.get().uri("/api/users/{login}", user.getLogin())
            .exchange()
            .expectStatus().isOk()
            .expectHeader().contentType(MediaType.APPLICATION_JSON)
            .expectBody()
            .jsonPath("$.login").isEqualTo(user.getLogin())
            .jsonPath("$.firstName").isEqualTo(DEFAULT_FIRSTNAME)
            .jsonPath("$.lastName").isEqualTo(DEFAULT_LASTNAME)
            .jsonPath("$.email").isEqualTo(DEFAULT_EMAIL)
            <%_ if (databaseType !== 'cassandra') { _%>
            .jsonPath("$.imageUrl").isEqualTo(DEFAULT_IMAGEURL)
            <%_ } _%>
            .jsonPath("$.langKey").isEqualTo(DEFAULT_LANGKEY);
        <%_ } _%>

        <%_ if (cacheProvider === 'memcached') { _%>
        if (!(cacheManager instanceof NoOpCacheManager)) {
            assertThat(cacheManager.getCache(UserRepository.USERS_BY_LOGIN_CACHE).get(user.getLogin())).isNotNull();
        }
        <%_ } else if (cacheManagerIsAvailable === true) { _%>
        assertThat(cacheManager.getCache(UserRepository.USERS_BY_LOGIN_CACHE).get(user.getLogin())).isNotNull();
        <%_ } _%>
    }

    @Test
    <%_ if (databaseType === 'sql' && !reactive) { _%>
    @Transactional
    <%_ } _%>
    void getNonExistingUser()<% if (!reactive) { %> throws Exception<% } %> {
        <%_ if (!reactive) { _%>
        restUserMockMvc.perform(get("/api/users/unknown"))
            .andExpect(status().isNotFound());
        <%_ } else { _%>
        webTestClient.get().uri("/api/users/unknown")
            .exchange()
            .expectStatus().isNotFound();
        <%_ } _%>
    }
<%_ if (authenticationType !== 'oauth2') { _%>

    @Test
    <%_ if (databaseType === 'sql' && !reactive) { _%>
    @Transactional
    <%_ } _%>
    void updateUser() throws Exception {
        // Initialize the database
        userRepository.save<% if (databaseType === 'sql' && !reactive) { %>AndFlush<% } %>(user)<% if (reactive) { %>.block()<% } %>;
        <%_ if (searchEngine === 'elasticsearch' && reactive) { _%>
        // Configure the mock search repository
        when(mockUserSearchRepository.save(any()))
            .thenAnswer(invocation -> Mono.just(invocation.getArgument(0)));
        <%_ } _%>
        int databaseSizeBeforeUpdate = userRepository.findAll()<% if (reactive) { %>
            .collectList().block()<% } %>.size();

        // Update the user
        <%= asEntity('User') %> updatedUser = userRepository.findById(user.getId()).<% if (reactive) { %>block<% } else { %>get<% } %>();

        ManagedUserVM managedUserVM = new ManagedUserVM();
        managedUserVM.setId(updatedUser.getId());
        managedUserVM.setLogin(updatedUser.getLogin());
        managedUserVM.setPassword(UPDATED_PASSWORD);
        managedUserVM.setFirstName(UPDATED_FIRSTNAME);
        managedUserVM.setLastName(UPDATED_LASTNAME);
        managedUserVM.setEmail(UPDATED_EMAIL);
        managedUserVM.setActivated(updatedUser.isActivated());
        <%_ if (databaseType !== 'cassandra') { _%>
        managedUserVM.setImageUrl(UPDATED_IMAGEURL);
        <%_ } _%>
        managedUserVM.setLangKey(UPDATED_LANGKEY);
        <%_ if (databaseType !== 'cassandra') { _%>
        managedUserVM.setCreatedBy(updatedUser.getCreatedBy());
        managedUserVM.setCreatedDate(updatedUser.getCreatedDate());
        managedUserVM.setLastModifiedBy(updatedUser.getLastModifiedBy());
        managedUserVM.setLastModifiedDate(updatedUser.getLastModifiedDate());
        <%_ } _%>
        managedUserVM.setAuthorities(Collections.singleton(AuthoritiesConstants.USER));

        <%_ if (!reactive) { _%>
        restUserMockMvc.perform(put("/api/users")
            .contentType(MediaType.APPLICATION_JSON)
            .content(TestUtil.convertObjectToJsonBytes(managedUserVM))<% if (testsNeedCsrf) { %>
            .with(csrf())<% } %>)
            .andExpect(status().isOk());
        <%_ } else { _%>
        webTestClient.put().uri("/api/users")
            .contentType(MediaType.APPLICATION_JSON)
            .bodyValue(TestUtil.convertObjectToJsonBytes(managedUserVM))
            .exchange()
            .expectStatus().isOk();
        <%_ } _%>

        // Validate the User in the database
        assertPersistedUsers(users -> {
            assertThat(users).hasSize(databaseSizeBeforeUpdate);
            <%= asEntity('User') %> testUser = users.stream().filter(usr -> usr.getId().equals(updatedUser.getId())).findFirst().get();
            assertThat(testUser.getFirstName()).isEqualTo(UPDATED_FIRSTNAME);
            assertThat(testUser.getLastName()).isEqualTo(UPDATED_LASTNAME);
            assertThat(testUser.getEmail()).isEqualTo(UPDATED_EMAIL);
            <%_ if (databaseType !== 'cassandra') { _%>
            assertThat(testUser.getImageUrl()).isEqualTo(UPDATED_IMAGEURL);
            <%_ } _%>
            assertThat(testUser.getLangKey()).isEqualTo(UPDATED_LANGKEY);
        });
    }

    @Test
    <%_ if (databaseType === 'sql' && !reactive) { _%>
    @Transactional
    <%_ } _%>
    void updateUserLogin() throws Exception {
        // Initialize the database
        userRepository.save<% if (databaseType === 'sql' && !reactive) { %>AndFlush<% } %>(user)<% if (reactive) { %>.block()<% } %>;
        <%_ if (searchEngine === 'elasticsearch' && reactive) { _%>
        // Configure the mock search repository
        when(mockUserSearchRepository.save(any()))
            .thenAnswer(invocation -> Mono.just(invocation.getArgument(0)));
        <%_ } _%>
        int databaseSizeBeforeUpdate = userRepository.findAll()<% if (reactive) { %>
            .collectList().block()<% } %>.size();

        // Update the user
        <%= asEntity('User') %> updatedUser = userRepository.findById(user.getId()).<% if (reactive) { %>block<% } else { %>get<% } %>();

        ManagedUserVM managedUserVM = new ManagedUserVM();
        managedUserVM.setId(updatedUser.getId());
        managedUserVM.setLogin(UPDATED_LOGIN);
        managedUserVM.setPassword(UPDATED_PASSWORD);
        managedUserVM.setFirstName(UPDATED_FIRSTNAME);
        managedUserVM.setLastName(UPDATED_LASTNAME);
        managedUserVM.setEmail(UPDATED_EMAIL);
        managedUserVM.setActivated(updatedUser.isActivated());
        <%_ if (databaseType !== 'cassandra') { _%>
        managedUserVM.setImageUrl(UPDATED_IMAGEURL);
        <%_ } _%>
        managedUserVM.setLangKey(UPDATED_LANGKEY);
        <%_ if (databaseType !== 'cassandra') { _%>
        managedUserVM.setCreatedBy(updatedUser.getCreatedBy());
        managedUserVM.setCreatedDate(updatedUser.getCreatedDate());
        managedUserVM.setLastModifiedBy(updatedUser.getLastModifiedBy());
        managedUserVM.setLastModifiedDate(updatedUser.getLastModifiedDate());
        <%_ } _%>
        managedUserVM.setAuthorities(Collections.singleton(AuthoritiesConstants.USER));

        <%_ if (!reactive) { _%>
        restUserMockMvc.perform(put("/api/users")
            .contentType(MediaType.APPLICATION_JSON)
            .content(TestUtil.convertObjectToJsonBytes(managedUserVM))<% if (testsNeedCsrf) { %>
            .with(csrf())<% } %>)
            .andExpect(status().isOk());
        <%_ } else { _%>
        webTestClient.put().uri("/api/users")
            .contentType(MediaType.APPLICATION_JSON)
            .bodyValue(TestUtil.convertObjectToJsonBytes(managedUserVM))
            .exchange()
            .expectStatus().isOk();
        <%_ } _%>

        // Validate the User in the database
        assertPersistedUsers(users -> {
            assertThat(users).hasSize(databaseSizeBeforeUpdate);
            <%= asEntity('User') %> testUser = users.stream().filter(usr -> usr.getId().equals(updatedUser.getId())).findFirst().get();
            assertThat(testUser.getLogin()).isEqualTo(UPDATED_LOGIN);
            assertThat(testUser.getFirstName()).isEqualTo(UPDATED_FIRSTNAME);
            assertThat(testUser.getLastName()).isEqualTo(UPDATED_LASTNAME);
            assertThat(testUser.getEmail()).isEqualTo(UPDATED_EMAIL);
            <%_ if (databaseType !== 'cassandra') { _%>
            assertThat(testUser.getImageUrl()).isEqualTo(UPDATED_IMAGEURL);
            <%_ } _%>
            assertThat(testUser.getLangKey()).isEqualTo(UPDATED_LANGKEY);
        });
    }

    @Test
    <%_ if (databaseType === 'sql' && !reactive) { _%>
    @Transactional
    <%_ } _%>
    void updateUserExistingEmail() throws Exception {
        // Initialize the database with 2 users
        userRepository.save<% if (databaseType === 'sql' && !reactive) { %>AndFlush<% } %>(user)<% if (reactive) { %>.block()<% } %>;
        <%_ if (searchEngine === 'elasticsearch') { _%>
        mockUserSearchRepository.save(user);
        <%_ } _%>

        <%= asEntity('User') %> anotherUser = new <%= asEntity('User') %>();
        <%_ if (databaseType === 'cassandra') { _%>
        anotherUser.setId(UUID.randomUUID().toString());
        <%_ } _%>
        anotherUser.setLogin("jhipster");
        anotherUser.setPassword(RandomStringUtils.random(60));
        anotherUser.setActivated(true);
        anotherUser.setEmail("jhipster@localhost");
        anotherUser.setFirstName("java");
        anotherUser.setLastName("hipster");
        <%_ if (databaseType !== 'cassandra') { _%>
        anotherUser.setImageUrl("");
        <%_ } _%>
        anotherUser.setLangKey("en");
        <%_ if (databaseType === 'sql' && reactive) { _%>
        anotherUser.setCreatedBy(Constants.SYSTEM_ACCOUNT);
        <%_ } _%>
        userRepository.save<% if (databaseType === 'sql' && !reactive) { %>AndFlush<% } %>(anotherUser)<% if (reactive) { %>.block()<% } %>;
        <%_ if (searchEngine === 'elasticsearch') { _%>
        mockUserSearchRepository.save(anotherUser);
        <%_ } _%>

        // Update the user
        <%= asEntity('User') %> updatedUser = userRepository.findById(user.getId()).<% if (reactive) { %>block<% } else { %>get<% } %>();

        ManagedUserVM managedUserVM = new ManagedUserVM();
        managedUserVM.setId(updatedUser.getId());
        managedUserVM.setLogin(updatedUser.getLogin());
        managedUserVM.setPassword(updatedUser.getPassword());
        managedUserVM.setFirstName(updatedUser.getFirstName());
        managedUserVM.setLastName(updatedUser.getLastName());
        managedUserVM.setEmail("jhipster@localhost");// this email should already be used by anotherUser
        managedUserVM.setActivated(updatedUser.isActivated());
        <%_ if (databaseType !== 'cassandra') { _%>
        managedUserVM.setImageUrl(updatedUser.getImageUrl());
        <%_ } _%>
        managedUserVM.setLangKey(updatedUser.getLangKey());
        <%_ if (databaseType !== 'cassandra') { _%>
        managedUserVM.setCreatedBy(updatedUser.getCreatedBy());
        managedUserVM.setCreatedDate(updatedUser.getCreatedDate());
        managedUserVM.setLastModifiedBy(updatedUser.getLastModifiedBy());
        managedUserVM.setLastModifiedDate(updatedUser.getLastModifiedDate());
        <%_ } _%>
        managedUserVM.setAuthorities(Collections.singleton(AuthoritiesConstants.USER));

        <%_ if (!reactive) { _%>
        restUserMockMvc.perform(put("/api/users")
            .contentType(MediaType.APPLICATION_JSON)
            .content(TestUtil.convertObjectToJsonBytes(managedUserVM))<% if (testsNeedCsrf) { %>
            .with(csrf())<% } %>)
            .andExpect(status().isBadRequest());
        <%_ } else { _%>
        webTestClient.put().uri("/api/users")
            .contentType(MediaType.APPLICATION_JSON)
            .bodyValue(TestUtil.convertObjectToJsonBytes(managedUserVM))
            .exchange()
            .expectStatus().isBadRequest();
        <%_ } _%>
    }

    @Test
    <%_ if (databaseType === 'sql' && !reactive) { _%>
    @Transactional
    <%_ } _%>
    void updateUserExistingLogin() throws Exception {
        // Initialize the database
        userRepository.save<% if (databaseType === 'sql' && !reactive) { %>AndFlush<% } %>(user)<% if (reactive) { %>.block()<% } %>;
        <%_ if (searchEngine === 'elasticsearch') { _%>
        mockUserSearchRepository.save(user);
        <%_ } _%>

        <%= asEntity('User') %> anotherUser = new <%= asEntity('User') %>();
        <%_ if (databaseType === 'cassandra') { _%>
        anotherUser.setId(UUID.randomUUID().toString());
        <%_ } _%>
        anotherUser.setLogin("jhipster");
        anotherUser.setPassword(RandomStringUtils.random(60));
        anotherUser.setActivated(true);
        anotherUser.setEmail("jhipster@localhost");
        anotherUser.setFirstName("java");
        anotherUser.setLastName("hipster");
        <%_ if (databaseType !== 'cassandra') { _%>
        anotherUser.setImageUrl("");
        <%_ } _%>
        anotherUser.setLangKey("en");
        <%_ if (databaseType === 'sql' && reactive) { _%>
        anotherUser.setCreatedBy(Constants.SYSTEM_ACCOUNT);
        <%_ } _%>
        userRepository.save<% if (databaseType === 'sql' && !reactive) { %>AndFlush<% } %>(anotherUser)<% if (reactive) { %>.block()<% } %>;
        <%_ if (searchEngine === 'elasticsearch') { _%>
        mockUserSearchRepository.save(anotherUser);
        <%_ } _%>

        // Update the user
        <%= asEntity('User') %> updatedUser = userRepository.findById(user.getId()).<% if (reactive) { %>block<% } else { %>get<% } %>();

        ManagedUserVM managedUserVM = new ManagedUserVM();
        managedUserVM.setId(updatedUser.getId());
        managedUserVM.setLogin("jhipster");// this login should already be used by anotherUser
        managedUserVM.setPassword(updatedUser.getPassword());
        managedUserVM.setFirstName(updatedUser.getFirstName());
        managedUserVM.setLastName(updatedUser.getLastName());
        managedUserVM.setEmail(updatedUser.getEmail());
        managedUserVM.setActivated(updatedUser.isActivated());
        <%_ if (databaseType !== 'cassandra') { _%>
        managedUserVM.setImageUrl(updatedUser.getImageUrl());
        <%_ } _%>
        managedUserVM.setLangKey(updatedUser.getLangKey());
        <%_ if (databaseType !== 'cassandra') { _%>
        managedUserVM.setCreatedBy(updatedUser.getCreatedBy());
        managedUserVM.setCreatedDate(updatedUser.getCreatedDate());
        managedUserVM.setLastModifiedBy(updatedUser.getLastModifiedBy());
        managedUserVM.setLastModifiedDate(updatedUser.getLastModifiedDate());
        <%_ } _%>
        managedUserVM.setAuthorities(Collections.singleton(AuthoritiesConstants.USER));

        <%_ if (!reactive) { _%>
        restUserMockMvc.perform(put("/api/users")
            .contentType(MediaType.APPLICATION_JSON)
            .content(TestUtil.convertObjectToJsonBytes(managedUserVM))<% if (testsNeedCsrf) { %>
            .with(csrf())<% } %>)
            .andExpect(status().isBadRequest());
        <%_ } else { _%>
        webTestClient.put().uri("/api/users")
            .contentType(MediaType.APPLICATION_JSON)
            .bodyValue(TestUtil.convertObjectToJsonBytes(managedUserVM))
            .exchange()
            .expectStatus().isBadRequest();
        <%_ } _%>
    }

    @Test
    <%_ if (databaseType === 'sql' && !reactive) { _%>
    @Transactional
    <%_ } _%>
    void deleteUser()<% if (!reactive) { %> throws Exception<% } %> {
        // Initialize the database
        userRepository.save<% if (databaseType === 'sql' && !reactive) { %>AndFlush<% } %>(user)<% if (reactive) { %>.block()<% } %>;
        <%_ if (searchEngine === 'elasticsearch' && reactive) { _%>
        // Configure the mock search repository
        when(mockUserSearchRepository.delete(any())).thenReturn(Mono.empty());
        <%_ } _%>
        int databaseSizeBeforeDelete = userRepository.findAll()<% if (reactive) { %>
            .collectList().block()<% } %>.size();

        // Delete the user
        <%_ if (!reactive) { _%>
        restUserMockMvc.perform(delete("/api/users/{login}", user.getLogin())
            .accept(MediaType.APPLICATION_JSON)<% if (testsNeedCsrf) { %>
            .with(csrf())<% } %>)
            .andExpect(status().isNoContent());
        <%_ } else { _%>
        webTestClient.delete().uri("/api/users/{login}", user.getLogin())
            .accept(MediaType.APPLICATION_JSON)
            .exchange()
            .expectStatus().isNoContent();
        <%_ } _%>
        <%_ if (cacheManagerIsAvailable === true) { _%>

        assertThat(cacheManager.getCache(UserRepository.USERS_BY_LOGIN_CACHE).get(user.getLogin())).isNull();
        <%_ } _%>

        // Validate the database is empty
        assertPersistedUsers(users -> assertThat(users).hasSize(databaseSizeBeforeDelete - 1));
    }
    <%_ } _%>
    <%_ if ((databaseType === 'sql' || databaseType === 'mongodb' || databaseType === 'neo4j' || databaseType === 'couchbase')
        && (!(authenticationType === 'oauth2' && applicationType === 'microservice'))) { _%>

    @Test
    <%_ if (databaseType === 'sql' && !reactive) { _%>
    @Transactional
    <%_ } _%>
    void getAllAuthorities()<% if (!reactive) { %> throws Exception<% } %> {
        <%_ if (!reactive) { _%>
        restUserMockMvc.perform(get("/api/users/authorities")
            .accept(MediaType.APPLICATION_JSON)
            .contentType(MediaType.APPLICATION_JSON))
            .andExpect(status().isOk())
            .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
            .andExpect(jsonPath("$").isArray())
            .andExpect(jsonPath("$").value(hasItems(AuthoritiesConstants.USER, AuthoritiesConstants.ADMIN)));
        <%_ } else { _%>
        webTestClient.get().uri("/api/users/authorities")
            .accept(MediaType.APPLICATION_JSON)
            .exchange()
            .expectStatus().isOk()
            .expectHeader().contentType(MediaType.APPLICATION_JSON_VALUE)
            .expectBody()
            .jsonPath("$").isArray()
            .jsonPath("$[?(@=='" + AuthoritiesConstants.ADMIN + "')]").hasJsonPath()
            .jsonPath("$[?(@=='" + AuthoritiesConstants.USER + "')]").hasJsonPath();
        <%_ } _%>
    }
    <%_ } _%>

    @Test
    void testUserEquals() throws Exception {
        TestUtil.equalsVerifier(<%= asEntity('User') %>.class);
        <%= asEntity('User') %> user1 = new <%= asEntity('User') %>();
        user1.setId(<% if (databaseType === 'sql' && authenticationType !== 'oauth2') { %>1L<% } else { %>"id1"<% } %>);
        <%= asEntity('User') %> user2 = new <%= asEntity('User') %>();
        user2.setId(user1.getId());
        assertThat(user1).isEqualTo(user2);
        user2.setId(<% if (databaseType === 'sql' && authenticationType !== 'oauth2') { %>2L<% } else { %>"id2"<% } %>);
        assertThat(user1).isNotEqualTo(user2);
        user1.setId(null);
        assertThat(user1).isNotEqualTo(user2);
    }

    @Test
    void testUserDTOtoUser() {
        <%= asDto('User') %> userDTO = new <%= asDto('User') %>();
        userDTO.setId(DEFAULT_ID);
        userDTO.setLogin(DEFAULT_LOGIN);
        userDTO.setFirstName(DEFAULT_FIRSTNAME);
        userDTO.setLastName(DEFAULT_LASTNAME);
        userDTO.setEmail(DEFAULT_EMAIL);
        userDTO.setActivated(true);
        <%_ if (databaseType !== 'cassandra') { _%>
        userDTO.setImageUrl(DEFAULT_IMAGEURL);
        <%_ } _%>
        userDTO.setLangKey(DEFAULT_LANGKEY);
        <%_ if (databaseType !== 'cassandra') { _%>
        userDTO.setCreatedBy(DEFAULT_LOGIN);
        userDTO.setLastModifiedBy(DEFAULT_LOGIN);
        <%_ } _%>
        userDTO.setAuthorities(Collections.singleton(AuthoritiesConstants.USER));

        <%= asEntity('User') %> user = userMapper.userDTOToUser(userDTO);
        assertThat(user.getId()).isEqualTo(DEFAULT_ID);
        assertThat(user.getLogin()).isEqualTo(DEFAULT_LOGIN);
        assertThat(user.getFirstName()).isEqualTo(DEFAULT_FIRSTNAME);
        assertThat(user.getLastName()).isEqualTo(DEFAULT_LASTNAME);
        assertThat(user.getEmail()).isEqualTo(DEFAULT_EMAIL);
        assertThat(user.isActivated()).isTrue();
        <%_ if (databaseType !== 'cassandra') { _%>
        assertThat(user.getImageUrl()).isEqualTo(DEFAULT_IMAGEURL);
        <%_ } _%>
        assertThat(user.getLangKey()).isEqualTo(DEFAULT_LANGKEY);
        <%_ if (databaseType !== 'cassandra') { _%>
        assertThat(user.getCreatedBy()).isNull();
        assertThat(user.getCreatedDate()).isNotNull();
        assertThat(user.getLastModifiedBy()).isNull();
        assertThat(user.getLastModifiedDate()).isNotNull();
        <%_ } _%>
        assertThat(user.getAuthorities())<% if (databaseType !== 'cassandra' && databaseType !== 'couchbase') { %>.extracting("name")<%_ } _%>.containsExactly(AuthoritiesConstants.USER);
    }

    @Test
    void testUserToUserDTO() {
        user.setId(DEFAULT_ID);
        <%_ if (databaseType !== 'cassandra') { _%>
        user.setCreatedBy(DEFAULT_LOGIN);
        user.setCreatedDate(Instant.now());
        user.setLastModifiedBy(DEFAULT_LOGIN);
        user.setLastModifiedDate(Instant.now());
        <%_ } _%>
        <%_ if (databaseType !== 'cassandra' && databaseType !== 'couchbase') { _%>
        Set<Authority> authorities = new HashSet<>();
        Authority authority = new Authority();
        authority.setName(AuthoritiesConstants.USER);
        authorities.add(authority);
        user.setAuthorities(authorities);
        <%_ } else { _%>
        user.setAuthorities(Stream.of(AuthoritiesConstants.USER).collect(Collectors.toSet()));
        <%_ } _%>

        <%= asDto('User') %> userDTO = userMapper.userToUserDTO(user);

        assertThat(userDTO.getId()).isEqualTo(DEFAULT_ID);
        assertThat(userDTO.getLogin()).isEqualTo(DEFAULT_LOGIN);
        assertThat(userDTO.getFirstName()).isEqualTo(DEFAULT_FIRSTNAME);
        assertThat(userDTO.getLastName()).isEqualTo(DEFAULT_LASTNAME);
        assertThat(userDTO.getEmail()).isEqualTo(DEFAULT_EMAIL);
        assertThat(userDTO.isActivated()).isTrue();
        <%_ if (databaseType !== 'cassandra') { _%>
        assertThat(userDTO.getImageUrl()).isEqualTo(DEFAULT_IMAGEURL);
        <%_ } _%>
        assertThat(userDTO.getLangKey()).isEqualTo(DEFAULT_LANGKEY);
        <%_ if (databaseType !== 'cassandra') { _%>
        assertThat(userDTO.getCreatedBy()).isEqualTo(DEFAULT_LOGIN);
        assertThat(userDTO.getCreatedDate()).isEqualTo(user.getCreatedDate());
        assertThat(userDTO.getLastModifiedBy()).isEqualTo(DEFAULT_LOGIN);
        assertThat(userDTO.getLastModifiedDate()).isEqualTo(user.getLastModifiedDate());
        <%_ } _%>
        assertThat(userDTO.getAuthorities()).containsExactly(AuthoritiesConstants.USER);
        assertThat(userDTO.toString()).isNotNull();
    }
    <%_ if (databaseType === 'sql' || databaseType === 'mongodb' || databaseType === 'neo4j') { _%>

    @Test
    void testAuthorityEquals() {
        Authority authorityA = new Authority();
        assertThat(authorityA)
          .isNotEqualTo(null)
          .isNotEqualTo(new Object());
        assertThat(authorityA.hashCode()).isZero();
        assertThat(authorityA.toString()).isNotNull();

        Authority authorityB = new Authority();
        assertThat(authorityA).isEqualTo(authorityB);

        authorityB.setName(AuthoritiesConstants.ADMIN);
        assertThat(authorityA).isNotEqualTo(authorityB);

        authorityA.setName(AuthoritiesConstants.USER);
        assertThat(authorityA).isNotEqualTo(authorityB);

        authorityB.setName(AuthoritiesConstants.USER);
        assertThat(authorityA)
          .isEqualTo(authorityB)
          .hasSameHashCodeAs(authorityB);
    }
    <%_ } _%>

    private void assertPersistedUsers(Consumer<List<<%= asEntity('User') %>>> userAssertion) {
        <%_ if (databaseType === 'couchbase') { _%>
        // The security filter chain clears the security context after remote calls
        SecurityContextHolder.setContext(TestSecurityContextHolder.getContext());
        <%_ } _%>
        userAssertion.accept(userRepository.findAll()<% if (reactive) { %>.collectList().block()<% } %>);
    }
}
